<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <title>考勤后台</title>
    <link rel="stylesheet" href="/paiban/base.css" />
    <script src="/static/plugs/jquery/jquery.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="/static/plugs/layer/layer.css">
    <script src="/static/plugs/layer/layer.js"></script>
  </head>
  <body>
      <div class="wrap">
      {include file="menu"}
      <div class="right">
          <form class="form">
              <table class="table">
              <tr>
                  <td>人员名称</td>
                  <td><input type="text" id="name" name="name" placeholder="如：张三" value="{if !empty($user_arr)}{$user_arr.name}{/if}"></td>
                  <td>部门</td>
                  <td><select id="bumen_id" name="bumen_id">
                      <option value='0'>所属部门</option>
                      {foreach $bumen as $v}
                      <option value='{$v.id}'{if !empty($user_arr)}{if $user_arr.bumen_id==$v.id} selected{/if}{/if}>{$v.name}</option>
                      {/foreach}
                      </select>
                  </td>
              </tr>
              <tr>
                  <td>账号</td>
                  <td><input type="text" id="username" name="username" placeholder="如：zhangsan" value="{if !empty($user_arr)}{$user_arr.username}{/if}"></td>
                  <td>密码</td>
                  <td><input type="text" id="password" name="password" placeholder="6位数以上密码" value="{if !empty($user_arr)}{$user_arr.password}{/if}"></td>
              </tr>
              <tr>
                  <td>部门管理员</td>
                  <td>
                      <input type="radio" name="is_manager" value="1"{if !empty($user_arr)}{if $user_arr.is_manager==1} checked{/if}{/if}>是
                      <input type="radio" name="is_manager" value="0"{if !empty($user_arr)}{if $user_arr.is_manager==0} checked{/if}{else} checked=""{/if}>否
                  </td>
                  <td>超级管理员</td>
                  <td>
                      <input type="radio" name="is_admin" value="1"{if !empty($user_arr)}{if $user_arr.is_admin==1} checked{/if}{/if}>是
                      <input type="radio" name="is_admin" value="0"{if !empty($user_arr)}{if $user_arr.is_admin==0} checked{/if}{else} checked=""{/if}>否
                  </td>
              </tr>
              <tr>
                  <td></td>
                  <td colspan="3"><button type="button" class="btn btn-primary" onclick="save()">保存</button>
                  <input type="hidden" id="id" name="id" value="{if !empty($user_arr)}{$user_arr.id}{/if}" />
                  </td>
              </tr>
          </table>
          </form>
          <div class="sp"></div>
          <table class="table">
              <tr>
                  <th>部门</th>
                  <th>姓名</th>
                  <th>账号名</th>
                  <th>部门管理员</th>
                  <th>超级管理员</th>
                  <th></th>
              </tr>
              {foreach($list as $v)}
              <tr>
                  <td>{$v.bumen.name}</td>
                  <td><a href="/paiban/admin/users/id/{$v.id}">{$v.name}</a></td>
                  <td>{$v.username}</td>
                  <td class="center">{if $v.is_manager==1}是{else}否{/if}</td>
                  <td class="center">{if $v.is_admin==1}是{else}否{/if}</td>
                  <td class="btn-cmd">
                      <a onclick="remove({$v.id})">删除</a>
                  </td>
              </tr>
              {/foreach}
          </table>
          {if !empty($list)}
          {$page|raw}
          {/if}
      </div>
      </div>
      <script>
          function save(){
              var formdata=$("form").serializeJson();
              console.log(formdata)
              $.post("/paiban/admin/usersave",formdata,function(res){
                  console.log(res)
                  if(res.code==1)
                  {
                      layer.msg('操作成功',{
                          time:1000,
                          end:function () {
                              location.reload()
                          }
                      })
                  }
              })
          }
          function remove(id){
              $.post("/paiban/admin/userremove",{id:id},function(res){
                  console.log(res)
                  if(res.code==1)
                  {
                      layer.msg('操作成功',{
                          time:1000,
                          end:function () {
                              location.reload()
                          }
                      })
                  }
              })
          }
       $.fn.serializeJson=function(){  
            var serializeObj={};  
            var array=this.serializeArray();  
            var str=this.serialize();  
            $(array).each(function(){  
                if(serializeObj[this.name]){   //如果属性已经存在
                    if($.isArray(serializeObj[this.name])){   
                        serializeObj[this.name].push(this.value); //已经有此数组，添加数据即可
                    }else{  
                        serializeObj[this.name]=[serializeObj[this.name],this.value];//变成数组，并添加当前遍历的value
                    }  
                }else{  
                    serializeObj[this.name]=this.value;//普通赋值。
                }  
            });  
            return serializeObj;  
        };
      </script>
  </body>
</html>